package com.glory.brepda.adapter;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.brepda.R;
import com.glory.brepda.base.MyApp;
import com.glory.brepda.entity.ScrapEntity;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_LogUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;

import java.util.List;

/**
 * FileName: newScrapListAdapter
 * Author: xuyifan902315
 * Date: 2025/2/21 11:43
 * Description:新报废码填报
 * History:
 */
public class newScrapListAdapter extends BaseQuickAdapter<ScrapEntity, BaseViewHolder> {

    private TextView tvTrackOutNum;
    private TextView tvLotYield;
    private TextView tvOrderYield;
    private TextView scrapNumSum;
    private TextView orderScrapNumSum;
    private TextView scrapPpmSum;
    private TextView tvLotTotalYield;
    private long totalNum = 0;
    private long orderScrapNum=0;
    private long orderMainQty=0;
    private long lotTotalScrap=0;
    public newScrapListAdapter(@Nullable List<ScrapEntity> data, TextView textView1,TextView textView2,TextView textView3,TextView textView4,TextView textView5,TextView textView6,TextView textView7,String TRACKINQTY,int osn,int omq,int ltn) {
        super(R.layout.new_item_scrap_list, data);
        this.tvTrackOutNum = textView1;
        this.tvLotYield = textView2;
        this.tvOrderYield = textView3;
        this.scrapNumSum = textView4;
        this.orderScrapNumSum = textView5;
        this.scrapPpmSum = textView6;
        this.tvLotTotalYield=textView7;
        this.totalNum = Long.parseLong(TRACKINQTY);
        this.orderScrapNum = osn;
        this.orderMainQty=omq;
        this.lotTotalScrap=ltn;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(BaseViewHolder helper, ScrapEntity item) {
        helper.setText(R.id.tvScrapDesc, item.getACTIONCODE()+"\n"+item.getACTIONREASON());
//        EditText etScrapNum = helper.getView(R.id.etScrapNum);
        helper.setText(R.id.etScrapNum,item.getScrapNum().equals("")?"0":item.getScrapNum());
        helper.setText(R.id.tvScrapPPM, item.getPpm());
        ImageView ivDelete = helper.getView(R.id.ivDelete);
//        etScrapNum.setText(item.getScrapNum().equals("")?"0":item.getScrapNum());
//        item.setMAINQTY("0");
//        item.setSUBQTY("0");
//        setEditListener(etScrapNum, item);

        tvTrackOutNum.setText(Calculation()<0?"0":Calculation()+"");

        ivDelete.setOnClickListener(view -> {
            remove(helper.getAdapterPosition());
            notifyDataSetChanged();
            long outputNum = Calculation()<0?0:Calculation();
            tvTrackOutNum.setText(outputNum+"");
            scrapNumSum.setText(calculateScrapNumSum());
            scrapPpmSum.setText(calculateScrapPpmSum());
            tvLotYield.setText(calculateYield());
            orderScrapNumSum.setText(calculateOrderScrapNum());
            tvOrderYield.setText(calculateOrderYield());
            tvLotTotalYield.setText(calculateLotYield());
        });

    }

    private String calculateLotYield() {
        double scrapNumSum = 0;
        double ton = Double.valueOf(tvTrackOutNum.getText().toString());
        for (ScrapEntity data : getData()) {
            scrapNumSum+=Integer.parseInt(data.getScrapNum());
        }
        double result = (ton)/(lotTotalScrap+scrapNumSum+ton)*100;
        String formatResult = String.format("%.2f",result)+"%";
        return formatResult;
    }

    private String calculateOrderYield() {
        double scrapNumSum = 0;
        for (ScrapEntity data : getData()) {
            scrapNumSum+=Integer.parseInt(data.getScrapNum());
        }
        double result = (orderMainQty-scrapNumSum)/orderMainQty*100;
        String formatResult = String.format("%.2f",result)+"%";
        return formatResult;
    }

    private String calculateOrderScrapNum() {
        int scrapNumSum = 0;
        for (ScrapEntity data : getData()) {
            scrapNumSum+=Integer.parseInt(data.getScrapNum());
        }
        return orderScrapNum+scrapNumSum+"";
    }

    private String calculateYield() {
        //出站数/进站数
        double ton = Double.valueOf(tvTrackOutNum.getText().toString());
        double scrapNum = Double.parseDouble(scrapNumSum.getText().toString());
        double result = ton/(ton+scrapNum)*100;
        String formatResult = String.format("%.2f",result)+"%";
        return formatResult;
    }

    private String calculateScrapPpmSum() {
        double scrapPpmSum = 0;
        for (ScrapEntity data : getData()) {
            scrapPpmSum+=Double.parseDouble(data.getPpm());
        }
        return String.format("%.2f",scrapPpmSum);
    }

    private String calculateScrapNumSum() {
        int scrapNumSum = 0;
        for (ScrapEntity data : getData()) {
            scrapNumSum+=Integer.parseInt(data.getScrapNum());
        }
        return Integer.toString(scrapNumSum);
    }


    public void setTotalNum(long mainQTY) {
        this.totalNum = mainQTY;
    }

    public long Calculation() {
        long scrapSum = 0;
        List<ScrapEntity> data = getData();

        if (data.size() <= 0) {
            return totalNum;
        }

        for (ScrapEntity entity : data) {
            scrapSum+=Long.parseLong(entity.getScrapNum());
        }

        if (scrapSum > totalNum) {
            scrapSum = -1;
        }

        return totalNum-scrapSum;
    }

}
